package unifieo.labs.sebo.dados;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import unifieo.labs.sebo.negocio.ControleException;
import unifieo.labs.sebo.negocio.Livro;
import unifieo.labs.sebo.negocio.Usuario;

public class ACBDSeboDerby implements IAcDados {
	
	private Connection BDCon = null;
	
	public boolean Conecta() 
	{
		String driver = "org.eclipse.derby.jdbc.EmbeddedDriver";
		String url = "jdbc:derby:/home/usuario/rede/02505/turmas/tads/ltp3/0702/workspace/Sebo_v1/bdsebo";
		try 
		{
			Class.forName(driver);
			BDCon = DriverManager.getConnection(url);
		} 
		catch (Exception e) 
		{
			return false;
		}
		return true;
	}

	public boolean AdicionarLivro(Livro novolivro) 
	{
		// TODO Auto-generated method stub
		return false;
	}

	public int SelecionaUltimoCodigoLivro() 
	{
		// TODO Auto-generated method stub
		return 0;
	}

	public Usuario SelecionaUsuario(String login) throws SQLException 
	{
		PreparedStatement stat = null;
		ResultSet rset = null;
		Usuario result = null;
		String qry = "SELECT CODUSUARIO, LOGIN, SENHA, TIPO FROM BDSEBO.USUARIOS WHERE LOGIN = ?";

		try {
			stat = BDCon.prepareStatement(qry);
			stat.setString(1, login);
			rset = stat.executeQuery();
			
			if(rset.next())
			{
				result.setCodigo(rset.getInt("CODUSUARIO"));
				result.setLogin(rset.getString("LOGIN"));
				result.setSenha(rset.getString("SENHA"));
				result.setLogin(rset.getString("TIPO"));
				
			}
			
			
		} catch (SQLException e) {
			throw new ControleException("Erro Base Dados:" + e.getMessage());
		}
		finally
		{
			if(rset != null) rset.close();
			if(stat != null) stat.close();
		}		

		return result;
	}
	

}
